package com.sojson.controller.po;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.base.impl.BaseController;
import com.sojson.config.exception.TransErrorCode;
import com.sojson.controller.po.param.verify.PostApplyParamVerify;
import com.sojson.entity.bo.export.MemberEp;
import com.sojson.entity.po.PostApply;
import com.sojson.entity.vo.PostApplyVo;
import com.sojson.entity.vo.PostVo;
import com.sojson.enums.EPostApplyIsHire;
import com.sojson.enums.EPostApplyPostsStatus;
import com.sojson.result.Result;
import com.sojson.service.PostApplyService;
import com.sojson.util.StringUtil;
import com.sojson.util.poi.ExcelUtil;
import com.sojson.util.token.TokenUtil;

import io.swagger.annotations.Api;

/**
 * 岗位应聘表控制器
 * 
 * @author liu
 * @date 2021-05-10
 */
@Api(tags = "岗位应聘表管理")
@RestController
@RequestMapping("/postApply")
public class PostApplyController extends BaseController<PostApply> {

    @Autowired
    private PostApplyService postApplyService;

    /**
     * 单条插入
     * 
     * @param postApply
     * @return
     */
    @PostMapping("/insert")
    @PreAuthorize("@ss.hasPermi('/postApply/insert')")
    public Object insert(PostApply postApply) {
        return Result.success(postApplyService.insert(postApply));
    }

    /**
     * 批量插入
     * 
     * @param postApply
     * @return
     */
    @PostMapping("/insertList")
    @PreAuthorize("@ss.hasPermi('/postApply/insertList')")
    public Object insertList(PostApply postApply) {
        return Result.success(postApplyService.insertList(postApply));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param postApply
     * @return
     */
    @DeleteMapping("/deleteByDto")
    @PreAuthorize("@ss.hasPermi('/postApply/deleteByDto')")
    public Object deleteByDto(PostApply postApply) {
        return Result.success(postApplyService.deleteByDto(postApply));
    }

    /**
     * 统计全部数量
     * 
     * @return
     */
    @GetMapping("/countAll")
    @PreAuthorize("@ss.hasPermi('/postApply/countAll')")
    public Object countAll() {
        return Result.success(postApplyService.countAll());
    }

    /**
     * 查询所有
     * 
     * @return
     */
    @GetMapping("/findAll")
    @PreAuthorize("@ss.hasPermi('/postApply/findAll')")
    public Object findAll() {
        return Result.success(postApplyService.findAll());
    }

    /**
     * 根据对象统计数量
     * 
     * @param postApply
     * @return
     */
    @GetMapping("/countByDto")
    @PreAuthorize("@ss.hasPermi('/postApply/countByDto')")
    public Object countByDto(PostApply postApply) {
        return Result.success(postApplyService.countByDto(postApply));
    }

    /**
     * 根据对象查询
     * 
     * @param postApply
     * @return
     */
    @GetMapping("/findByDto")
    @PreAuthorize("@ss.hasPermi('/postApply/findByDto')")
    public Object findByDto(PostApply postApply) {
        List<PostApply> findByDto = postApplyService.findByDto(postApply);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById")
    @PreAuthorize("@ss.hasPermi('/postApply/deleteById')")
    public Object deleteById(Integer id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(postApplyService.deleteById(id));
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteByIdList")
    @PreAuthorize("@ss.hasPermi('/postApply/deleteByIdList')")
    public Object deleteByIdList(@RequestParam("ids") List<Integer> ids) {
        if (ids.size() == 0) {
            return Result.success();
        }

        return Result.success(postApplyService.deleteByIdList(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param postApply
     * @return
     */
    @PutMapping("/updateById")
    @PreAuthorize("@ss.hasPermi('/postApply/updateById')")
    public Object updateById(PostApply postApply) {
        return Result.success(postApplyService.updateById(postApply));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param postApply
     * @return
     */
    @PutMapping("/updateStaticByIdList")
    @PreAuthorize("@ss.hasPermi('/postApply/updateStaticByIdList')")
    public Object updateStaticByIdList(PostApply postApply) {
        return Result.success(postApplyService.updateStaticByIdList(postApply));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param postApply
     * @return
     */
    @PutMapping("/updateChangeByIdList")
    @PreAuthorize("@ss.hasPermi('/postApply/updateChangeByIdList')")
    public Object updateChangeByIdList(PostApply postApply) {
        return Result.success(postApplyService.updateChangeByIdList(postApply));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param postApply
     * @return
     */
    @PutMapping("/updatePartialById")
    @PreAuthorize("@ss.hasPermi('/postApply/updatePartialById')")
    public Object updatePartialById(PostApply postApply) {
        return Result.success(postApplyService.updatePartialById(postApply));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param postApply
     * @return
     */
    @PutMapping("/updateStaticPartialByIdList")
    @PreAuthorize("@ss.hasPermi('/postApply/updateStaticPartialByIdList')")
    public Object updateStaticPartialByIdList(PostApply postApply) {
        return Result.success(postApplyService.updateStaticPartialByIdList(postApply));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param postApply
     * @return
     */
    @PutMapping("/updateChangePartialByIdList")
    @PreAuthorize("@ss.hasPermi('/postApply/updateChangePartialByIdList')")
    public Object updateChangePartialByIdList(PostApply postApply) {
        return Result.success(postApplyService.updateChangePartialByIdList(postApply));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @GetMapping("/findById")
    @PreAuthorize("@ss.hasPermi('/postApply/findById')")
    public Object findById(Integer id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(postApplyService.findById(id));
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     */
    @GetMapping("/findByIdList")
    @PreAuthorize("@ss.hasPermi('/postApply/findByIdList')")
    public Object findByIdList(@RequestParam("ids[]") List<Integer> ids) {
        if (ids.size() == 0) {
            return Result.success();
        }

        return Result.success(postApplyService.findByIdList(ids));
    }

    /**
     * 通过对象查询id列表
     * 
     * @param postApply
     * @return
     */
    @GetMapping("/findIdByDto")
    @PreAuthorize("@ss.hasPermi('/postApply/findIdByDto')")
    public Object findIdByDto(PostApply postApply) {
        return Result.success(postApplyService.findIdByDto(postApply));
    }

    /**
     * 通过userId删除(物理删除)
     * 
     * @param userId
     * @return
     */
    @DeleteMapping("/deleteByUserId")
    @PreAuthorize("@ss.hasPermi('/postApply/deleteByUserId')")
    public Object deleteByUserId(Integer userId) {
        if (userId == null) {
            return Result.success();
        }

        return Result.success(postApplyService.deleteByUserId(userId));
    }

    /**
     * 通过userId列表删除(物理删除)
     * 
     * @param userIds
     * @return
     */
    @DeleteMapping("/deleteByUserIdList")
    @PreAuthorize("@ss.hasPermi('/postApply/deleteByUserIdList')")
    public Object deleteByUserIdList(@RequestParam("userIds") List<Integer> userIds) {
        if (userIds.size() == 0) {
            return Result.success();
        }

        return Result.success(postApplyService.deleteByUserIdList(userIds));
    }

    /**
     * 通过userId更新(全部更新)
     * 
     * @param postApply
     * @return
     */
    @PutMapping("/updateByUserId")
    @PreAuthorize("@ss.hasPermi('/postApply/updateByUserId')")
    public Object updateByUserId(PostApply postApply) {
        return Result.success(postApplyService.updateByUserId(postApply));
    }

    /**
     * 通过userId列表更新(全部更新)(更新内容相同)
     * 
     * @param postApply
     * @return
     */
    @PutMapping("/updateStaticByUserIdList")
    @PreAuthorize("@ss.hasPermi('/postApply/updateStaticByUserIdList')")
    public Object updateStaticByUserIdList(PostApply postApply) {
        return Result.success(postApplyService.updateStaticByUserIdList(postApply));
    }

    /**
     * 通过userId列表更新(全部更新)(更新内容不相同)
     * 
     * @param postApplys
     * @return
     */
    @PutMapping("/updateChangeByUserIdList")
    @PreAuthorize("@ss.hasPermi('/postApply/updateChangeByUserIdList')")
    public Object updateChangeByUserIdList(PostApply postApplys) {
        return Result.success(postApplyService.updateChangeByUserIdList(postApplys));
    }

    /**
     * 通过userId更新(局部更新)
     * 
     * @param postApply
     * @return
     */
    @PutMapping("/updatePartialByUserId")
    @PreAuthorize("@ss.hasPermi('/postApply/updatePartialByUserId')")
    public Object updatePartialByUserId(PostApply postApply) {
        return Result.success(postApplyService.updatePartialByUserId(postApply));
    }

    /**
     * 通过userId列表更新(局部更新)(更新内容相同)
     * 
     * @param postApply
     * @return
     */
    @PutMapping("/updateStaticPartialByUserIdList")
    @PreAuthorize("@ss.hasPermi('/postApply/updateStaticPartialByUserIdList')")
    public Object updateStaticPartialByUserIdList(PostApply postApply) {
        return Result.success(postApplyService.updateStaticPartialByUserIdList(postApply));
    }

    /**
     * 通过userId列表更新(局部更新)(更新内容不相同)
     * 
     * @param postApplys
     * @return
     */
    @PutMapping("/updateChangePartialByUserIdList")
    @PreAuthorize("@ss.hasPermi('/postApply/updateChangePartialByUserIdList')")
    public Object updateChangePartialByUserIdList(PostApply postApplys) {
        return Result.success(postApplyService.updateChangePartialByUserIdList(postApplys));
    }

    /**
     * 通过userId查询
     * 
     * @param userId
     * @return
     */
    @GetMapping("/findByUserId")
    @PreAuthorize("@ss.hasPermi('/postApply/findByUserId')")
    public Object findByUserId(Integer userId) {
        if (userId == null) {
            return Result.success();
        }

        return Result.success(postApplyService.findByUserId(userId));
    }

    /**
     * 通过userId列表查询
     * 
     * @param userIds
     * @return
     */
    @GetMapping("/findByUserIdList")
    @PreAuthorize("@ss.hasPermi('/postApply/findByUserIdList')")
    public Object findByUserIdList(@RequestParam("userIds[]") List<Integer> userIds) {
        if (userIds.size() == 0) {
            return Result.success();
        }

        return Result.success(postApplyService.findByUserIdList(userIds));
    }

    /**
     * 通过对象查询userId列表
     * 
     * @param postApply
     * @return
     */
    @GetMapping("/findUserIdByDto")
    @PreAuthorize("@ss.hasPermi('/postApply/findUserIdByDto')")
    public Object findUserIdByDto(PostApply postApply) {
        return Result.success(postApplyService.findUserIdByDto(postApply));
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     */
    @DeleteMapping("/clear")
    @PreAuthorize("@ss.hasPermi('/postApply/clear')")
    public Object clear() {
        postApplyService.clear();
        return Result.success();
    }

    /**
     * 应聘岗位
     * 
     * @param postApply
     * @return
     * @throws Exception 
     */
    @PostMapping("/apply")
    @PreAuthorize("@ss.login()")
    public Object apply(@RequestBody PostApply postApply) throws Exception {
        // 校验参数
        Result<?> paramResult = PostApplyParamVerify.apply(postApply);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        postApply.insertInit();
        return Result.success(postApplyService.apply(postApply));
    }

    /**
     * 取消应聘
     * 
     * @param id ID
     * @return
     * @throws Exception
     */
    @PutMapping("/cancel/{id}")
    @PreAuthorize("@ss.login()")
    public Object cancel(@PathVariable Integer id) throws Exception {
        if (StringUtil.isBlankObject(id)) {
            return Result.fail("ID不能为空");
        }

        return Result.success(postApplyService.cancel(id));
    }

    /**
     * 查看应聘的人列表
     * 
     * @param postApply
     * @return
     */
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('/postApply/list')")
    public Object list(PostApplyVo postApply) throws Exception {
        List<PostApplyVo> list = postApplyService.list(postApply);
        return Result.success(list, list);
    }

    /**
     * 中介查看应聘的人列表
     * 
     * @param postApply
     * @return
     */
    @GetMapping("/listApp")
    @PreAuthorize("@ss.hasPermi('/postApply/listApp')")
    public Object listApp(PostApplyVo postApply) throws Exception {
        postApply.setPostUserId(TokenUtil.getUserId());
        List<PostApplyVo> list = postApplyService.list(postApply);
        return Result.success(list, list);
    }

    /**
     * 中介导出应聘的人列表
     * 
     * @param postApply
     * @return
     */
    @GetMapping("/listAppExport")
    @PreAuthorize("@ss.hasPermi('/postApply/listApp')")
    public Object listAppExport(PostApplyVo postApply) throws Exception {
        postApply.setPostUserId(TokenUtil.getUserId());
        List<MemberEp> listExport = postApplyService.listExport(postApply);

        ExcelUtil<MemberEp> util = new ExcelUtil<>(MemberEp.class);
        return util.exportExcel(listExport, "用户数据");
    }

    /**
     * 中介统计应聘的人
     * 
     * @param postApply
     * @return
     */
    @GetMapping("/countApp")
    @PreAuthorize("@ss.hasPermi('/postApply/countApp')")
    public Object countApp(PostApplyVo postApply) throws Exception {
        postApply.setPostUserId(TokenUtil.getUserId());
        return Result.success(postApplyService.count(postApply));
    }

    /**
     * 中介统计应聘的人
     * 
     * @param postApply
     * @return
     */
    @GetMapping("/countAllTypeApp")
    @PreAuthorize("@ss.hasPermi('/postApply/countApp')")
    public Object countAllTypeApp(PostApplyVo postApply) throws Exception {
        Map<String, Integer> hashMap = new HashMap<>();

        // 所有申请的人数
        postApply.setPostUserId(TokenUtil.getUserId());
        postApply.setAscOrDesc(null);
        int count = postApplyService.count(postApply);
        hashMap.put("all", count);

        // 录用的人数
        postApply.setIsHire(EPostApplyIsHire.HIRE_YES.getCode());
        count = postApplyService.count(postApply);
        hashMap.put("hireYes", count);

        // 上岗的人数
        postApply.setPostsStatus(EPostApplyPostsStatus.POSTS_YES.getCode());
        count = postApplyService.count(postApply);
        hashMap.put("postsYes", count);
        postApply.setPostsStatus(null);

        // 招到的人数,录用的不包含取消的
        List<Integer> arrayList = new ArrayList<>();
        arrayList.add(EPostApplyPostsStatus.AUDIT.getCode());
        arrayList.add(EPostApplyPostsStatus.POSTS_YES.getCode());
        arrayList.add(EPostApplyPostsStatus.POSTS_NO.getCode());
        postApply.setPostsStatuss(arrayList);
        postApply.setIsHire(EPostApplyIsHire.HIRE_YES.getCode());
        count = postApplyService.count(postApply);
        hashMap.put("recruitYes", count);

        return Result.success(hashMap);
    }

    /**
     * 录用
     * 
     * @param postApply
     * @return
     */
    @PutMapping("/hireYes")
    @PreAuthorize("@ss.hasPermi('/postApply/hireYes')")
    public Object hireYes(@RequestBody PostApply postApply) throws Exception {
        if (StringUtil.isBlankObject(postApply.getId())) {
            return Result.fail("ID不能为空");
        }
        return Result.success(postApplyService.hireYes(postApply));
    }

    /**
     * 不录用
     * 
     * @param postApply
     * @return
     */
    @PutMapping("/hireNo")
    @PreAuthorize("@ss.hasPermi('/postApply/hireNo')")
    public Object hireNo(@RequestBody PostApply postApply) throws Exception {
        if (StringUtil.isBlankObject(postApply.getId())) {
            return Result.fail("ID不能为空");
        }
        return Result.success(postApplyService.hireNo(postApply));
    }

    /**
     * 已上岗
     * 
     * @param postApply
     * @return
     */
    @PutMapping("/postsYes")
    @PreAuthorize("@ss.hasPermi('/postApply/postsYes')")
    public Object postsYes(@RequestBody PostApply postApply) throws Exception {
        if (StringUtil.isBlankObject(postApply.getId())) {
            return Result.fail("ID不能为空");
        }
        return Result.success(postApplyService.postsYes(postApply));
    }

    /**
     * 未上岗
     * 
     * @param postApply
     * @return
     */
    @PutMapping("/postsNo")
    @PreAuthorize("@ss.hasPermi('/postApply/postsNo')")
    public Object postsNo(@RequestBody PostApply postApply) throws Exception {
        if (StringUtil.isBlankObject(postApply.getId())) {
            return Result.fail("ID不能为空");
        }
        return Result.success(postApplyService.postsNo(postApply));
    }

    /**
     * 已上岗
     * 
     * @param postApply
     * @return
     */
    @PutMapping("/postsYesWeb")
    @PreAuthorize("@ss.hasPermi('/postApply/postsYesWeb')")
    public Object postsYesWeb(@RequestBody PostApply postApply) throws Exception {
        if (StringUtil.isBlankObject(postApply.getId())) {
            return Result.fail("ID不能为空");
        }
        return Result.success(postApplyService.postsYesWeb(postApply));
    }

    /**
     * 未上岗
     * 
     * @param postApply
     * @return
     */
    @PutMapping("/postsNoWeb")
    @PreAuthorize("@ss.hasPermi('/postApply/postsNoWeb')")
    public Object postsNoWeb(@RequestBody PostApply postApply) throws Exception {
        if (StringUtil.isBlankObject(postApply.getId())) {
            return Result.fail("ID不能为空");
        }
        return Result.success(postApplyService.postsNoWeb(postApply));
    }

    /**
     * 用户查看应聘的岗位
     * 
     * @param postApply
     * @return
     */
    @GetMapping("/listAppUser")
    @PreAuthorize("@ss.hasPermi('/postApply/listAppUser')")
    public Object listAppUser(PostApplyVo postApply) throws Exception {
        postApply.setUserId(TokenUtil.getUserId());
        List<PostVo> list = postApplyService.listPost(postApply);
        return Result.success(list, list);
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @GetMapping("/getInfo/{id}")
    public Result<PostApplyVo> getInfo(@PathVariable Integer id) {
        PostApplyVo getInfo = postApplyService.getInfo(id);
        return Result.success(getInfo);
    }

}